package org.vectrics.common.view;

/**
 * @author Mike Moore
 * @version 1.0
 */

import org.apache.log4j.Category;
import org.vectrics.common.html.*;
//import org.vectrics.common.controller.*;
import org.apache.ecs.html.Input;
import org.apache.ecs.html.TextArea;
import java.util.Properties;
import java.util.*;


public class SimpleForm extends Form {
    private static Category log = Category.getInstance(SimpleForm.class);
   // public static String FONT_PAGE_HEADER = "hdlnMedSltBlu";
    public static String SECTION_DESC_FONT = "stdFont";
    public static String SECTION_FONT = "header2";
    public static String FONT_REGISTER_FIELD_LABEL = "stdFont";
    public static String FONT_REGISTER_FIELD_VALUE = "stdFont";
    public static String COLOR_FIELD_VALUE_BORDER = "#90a0b0";
    public static String COLOR_FIELD_VALUE_BG = "#e8e8ec";
    public static String FONT_REGISTER_FIELD_LABEL_ERROR = "stdErrorFont";
    private Table mainTable = null;
    private static int COL_COUNT = 2;
    private Cell spcrCell1 = null;
    private Cell spcrCell2 = null;
    private boolean fReadOnly = false;
    
    private String labelClass = "formLabelClass";
    private String editClass = "formEditClass";
    private String comboClass = "formComboClass";
    private String dateFieldClass = "formDateFieldClass";
    private Properties params = null;
	private Properties invalidParams = null;
    
    
    public SimpleForm(Properties props, Properties invalidProps) {
    	super();
    	params = props;
    	invalidParams = invalidProps;
    	if (invalidParams == null) {
    		invalidParams = new Properties();
    	}
    	
		mainTable = new Table();
		mainTable.setCellPadding(1);
		mainTable.setCellSpacing(0);
        
		addElement(mainTable);
    }
    
/*
    public SimpleForm(Request request) {
	    super();
	    this.invalidParams = request.getInvalidParams();
	    params = request.getParameters();
	    mainTable = new Table();
        mainTable.setCellPadding(1);
        mainTable.setCellSpacing(0);
        
	    addElement(mainTable);
    }
*/    
    
    public void setTable(Table table) {
        mainTable = table;
    }
    
    public void setReadOnly(boolean value) {
        fReadOnly = value;
    }
    
    public boolean getReadOnly() {
        return(fReadOnly);
    }
    
    public void addReadOnlyText(String caption, String value) {
        Row row = mainTable.addRow();
        addLabelCell(row, "User", false, false);
        Cell valueCell = row.addCell();
        valueCell.setVAlign("middle");
        valueCell.setClass("ltGray");
        valueCell.addElement(TextHelper.getFont(labelClass, "&nbsp;"
                + value  + ""));
        addBlankRow();
    }

    public Cell addLabelCell(Row row, String caption, boolean required, boolean invalid) {
        Cell labelCell = row.addCell();
        labelCell.setVAlign("top");
        String fontClass = FONT_REGISTER_FIELD_LABEL;

        if (invalid)
            fontClass = FONT_REGISTER_FIELD_LABEL_ERROR;

        String reqStr = "";
        if ((required) && (!fReadOnly))
            reqStr = "<b> *</b>";

        labelCell.addElement(TextHelper.getFont(labelClass, "&nbsp;"
                + caption + reqStr));
        //addBlankRow();
        return(labelCell);
    }

    
    public Cell addLabelCell(Row row, String paramName, String caption, boolean required) {
        boolean invalid = false;
        if (this.getInvalidParam(paramName) != null)
            invalid = true;
        return(addLabelCell(row, caption, required, invalid));
    }

    public Input makeCheckBox(String name) {
        Input titleEdit = new Input();
        titleEdit.setType(Input.checkbox);
        titleEdit.setName(name);
        if (this.getParam(name).startsWith("t"))
            titleEdit.setChecked(true);
        else
            titleEdit.setChecked(false);
        return(titleEdit);
    }

    
    
    public void addCheckbox(String caption, String fldName, boolean required) {
        String paramValue = getParam(fldName);
        boolean value = false;
        if (paramValue != null) {
            if (paramValue.toLowerCase().startsWith("t")) {
                value = true;
            }
        }
        if (fReadOnly) {
            if (value) {
                makeLabelRow(caption, "True");
            }
            else {
                makeLabelRow(caption, "False");
            }
        }
        else {
            Cell checkCell = addEntryRow(fldName, caption, required);
            Input checkbox = makeCheckBox(fldName);
            checkCell.addElement(checkbox);
        }
    }

    public void addInputRow(String caption, String paramName, Input htmlElement, boolean required) {
        Cell elementCell = addEntryRow(paramName, caption, required);
        htmlElement.setName(paramName);
        elementCell.addElement(htmlElement);
    }

    public void addComboRow(String caption, String paramName, Combo htmlElement, boolean required) {
        Cell elementCell = addEntryRow(paramName, caption, required);
        htmlElement.setName(paramName);
        elementCell.addElement(htmlElement);
    }

    public void addTextAreaRow(String caption, String paramName, TextArea htmlElement, boolean required) {
        Cell elementCell = addEntryRow(paramName, caption, required);
        htmlElement.setName(paramName);
        elementCell.addElement(htmlElement);
    }
    
    
    
    public String getInvalidParam(String paramName) {
        String desc = invalidParams.getProperty(paramName);
        if (log.isDebugEnabled()) {
            log.debug("getInvalidParam(" + paramName + ") = " + desc);
        }
        return(desc);
    }
    
    
    public void addMonthYearEdit(String caption, String paramName, boolean required) {
        Cell elementCell = addEntryRow(paramName, caption, required);
        elementCell.addElement("Month&nbsp;");
        elementCell.addElement(makeEdit(paramName + "_month", "width:80px"));
        elementCell.addElement("&nbsp;");
        elementCell.addElement("&nbsp;&nbsp;Year&nbsp;");
        elementCell.addElement(makeEdit(paramName + "_year", "width:80px"));
    }

    public void setEditClass(String value) {
        editClass = value;
    }
    public Input makeEdit(String name, String style) {
        Input titleEdit = new Input();
        titleEdit.setType(Input.text);
        titleEdit.setName(name);
        titleEdit.setClass(editClass);
        titleEdit.setValue(getParam(name));
        
        return(titleEdit);
    }
    
    public Input makeEdit(String name) {
        return(makeEdit(name, null));
    }

    public Input makePasswordEdit(String name) {
        Input titleEdit = makeEdit(name);
        titleEdit.setType(Input.password);
        return(titleEdit);
    }

    public void addBlankRow() {
        Row row = mainTable.addRow();
        Cell cell = row.addCell();
        cell.setColSpan(COL_COUNT);
        Table fillTable = new Table();
        fillTable.setCellPadding(3);
        fillTable.setCellSpacing(0);
        fillTable.addRow().addCell();
        cell.addElement(fillTable);        
    }

    public Cell addEntryRow(String paramName, String caption, boolean required) {
        Row row = mainTable.addRow();
        addLabelCell(row, paramName, caption, required);
        Cell inputCell = row.addCell();
        inputCell.setVAlign("top");
        //addBlankRow();
        
        return(inputCell);
    }


    public void addSection(String caption, String desc) {
        addBlankRow();
        Row row = mainTable.addRow();
        Cell cell = row.addCell();
        cell.setColSpan(2);
        cell.addElement(TextHelper.getFont(SECTION_FONT, caption + "<br><br>"));
        if (desc != null) {
            cell.addElement(TextHelper.getFont(SECTION_DESC_FONT, desc + "<br><br>"));
        };

    }
    
    
    public void makeCheckBoxRow(String paramName, String caption, boolean required) {
        addCheckbox(caption, paramName, required);
    }
    
    public Input makeEditRow(String paramName, String caption, boolean required) {
        Input editBox = null;
        if (fReadOnly) {
            makeLabelRow(caption, getParam(paramName)); 
        }
        else {
            Row row = mainTable.addRow();
            addLabelCell(row, paramName, caption, required);
            Cell cell = row.addCell();
            cell.setVAlign("top");
            editBox = makeEdit(paramName);
            cell.addElement(editBox);
        }
        return(editBox);
    }

    public void makeLabelRow(String caption, String value) {
        Row row = mainTable.addRow();
        Cell labelCell = addLabelCell(row, "", caption, false);
        labelCell.setWidth("50%");
        Cell cell = row.addCell();
        cell.setBgColor(COLOR_FIELD_VALUE_BORDER);
        cell.setVAlign("top");
        
        Table innerTable = new Table();
        innerTable.setWidth("100%");
        //innerTable.setHeight("100%");
        innerTable.setCellSpacing(1);
        innerTable.setCellPadding(0);
        cell.addElement(innerTable);
        Row innerRow = innerTable.addRow();
        
        Cell innerCell = innerRow.addCell();
        innerCell.setBgColor(COLOR_FIELD_VALUE_BG);
        innerCell.addElement(TextHelper.getFont(labelClass, "&nbsp;" + value));
        innerCell.setWidth("50%");
    }


    public void makePasswordEditRow(String paramName, String caption, boolean required) {
        if (fReadOnly) {
            makeLabelRow(caption, "********"); 
        }
        else {
            Row row = mainTable.addRow();
            addLabelCell(row, paramName, caption, required);
            Cell cell = row.addCell();
            cell.setVAlign("top");
            cell.addElement(makePasswordEdit(paramName));
        }
    }

    public void showInvalidParams() {
        if (this.invalidParams.size() > 0) {
            log.debug("Invalid properties.  Count = " + invalidParams.size());
	    Row row = mainTable.addRow();
	    Cell mainCell = row.addCell();
	    mainCell.setColSpan(COL_COUNT);
            mainCell.addElement(TextHelper.getFont(FONT_REGISTER_FIELD_LABEL_ERROR,
                    "<br><b>Invalid Entries:</b>"));
            Enumeration invalidEnum = invalidParams.keys();
            while (invalidEnum.hasMoreElements()) {
                String name = (String)invalidEnum.nextElement();
                String errStr = invalidParams.getProperty(name);
                mainCell.addElement(TextHelper.getFont(FONT_REGISTER_FIELD_LABEL_ERROR,
                        "<br>&nbsp;&nbsp;*" + errStr));
            }
        }
        else {
            log.debug("No invalid properties");
        }
    }

    public void addSubmitButton(String caption, String cancelCaption) {
        Row buttonRow = mainTable.addRow();
        Cell buttonCell = buttonRow.addCell();
        buttonCell.setAlign("left");
        buttonCell = buttonRow.addCell();
        Input submitButton = new Input();
        buttonCell.setAlign("left");
        buttonCell.addElement(submitButton);
        submitButton.setType(Input.submit);
        submitButton.setValue(caption);
        submitButton.setName("submit");

        if (cancelCaption != null) {
            buttonCell.addElement("&nbsp;");
            Input cancelButton = new Input();
            buttonCell.addElement(cancelButton);
            cancelButton.setType(Input.submit);
            cancelButton.setValue(cancelCaption);
            cancelButton.setName("cancel");
        }
    }

    
    protected String getParam(String paramName) {
        String value = params.getProperty(paramName);
        if (value == null) {
            value ="";
        }
        return(value);
    }

}
